package gamecontroler;


import Ultilies.BitmapCollection;
import Ultilies.BitmapSynchroniser;
import Ultilies.SoundFactory;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

import com.example.boidpiranha.R;


public class MainActivity extends Activity implements OnTouchListener {

	private GameLoop gameLoop;

	private static boolean isPress;
	
	private static boolean isTouch;

	private static PointF touch= new PointF(-100, -100);
	
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);


		gameLoop = new GameLoop(this);// set draw component here

		gameLoop.setOnTouchListener(this);// set touch component here

		setInitialValues();
		
		gameLoop.load();// this to load data required
	}

	private void setInitialValues() {
		// TODO Auto-generated method stub
		setContentView(gameLoop);

		BitmapCollection bitmapCollection = new BitmapCollection();

		Bitmap standardBitmap = bitmapCollection.getBackground(getResources());

		Display display = getWindowManager().getDefaultDisplay();

		BitmapSynchroniser.setInitialParameters(display, standardBitmap);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {

		case MotionEvent.ACTION_DOWN:
			float tempX = event.getX();
			float tempY = event.getY();
			touch.x = (int) tempX;
			touch.y = (int) tempY;
			isPress = true;
			break;
		case MotionEvent.ACTION_UP:
			isPress = false;
			break;
		}
		return true;
	}

	@Override
	protected void onPause() {
		super.onPause();
		gameLoop.onPause();
	}

	@Override
	protected void onStart() {
		SoundFactory.playMusic();
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
		gameLoop.onResume();
	}

	public static int getTouchX() {
		return (int) touch.x;
	}

	public static int getTouchY() {
		return (int) touch.y;
	}

	public static void resetXY() {
		touch.set(-100, -100);
	}

	public static boolean isPress() {
		if ((touch.x>0)&&(touch.y)>0) return true;
		return false;
	}

	public void startIntent(Intent intent) {
		startActivity(intent);
		// finish();
	}

	public void exitGame() {
//		SoundFactory.stopMusic();
		finish();
	}

	public static PointF getTouch() {
		// TODO Auto-generated method stub
		return touch;
	}

}
